﻿@*角色绑定到菜单*@
@using Model.WaiGaoQiao;
@{
    ViewBag.Title = "AddRoleToModular";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
@section Head
{
    <script language="javascript" type="text/javascript">
        $(document).ready(function () {
            $("#btnSave").click(function () {
                var rowcount = 0;
                var roleid = @ViewData["selrole"];
                ClearBindModularToRole(roleid);
                var items = $("input[type='checkbox']");
                for (var i = 0; i < items.length; i++) {
                    var muid = $(items[i]).attr("name");
                    if ($(items[i]).attr("checked"))
                    {                                                                 
                        $.ajax({
                            type: "POST",
                            url: '@Url.Action("BindMenuToRole")',
                            data: { mid: muid, roleid: roleid },
                            dataType: "json",
                            async: false,
                            success: function (data) {
                                if (data) {
                                    rowcount = rowcount + ((data == -1) ? 0 : data);
                                }
                            },
                            error: function () { }
                        });   // end of ajax
                    }   // end of if               
                }
                showLoadingMsg("共有" + rowcount.toString() + "个模块绑定到角色!", { left: 200, top: 6 }, true, 3000);
            });
            $("#btnCancel").click(function(){
                CloseModelWindow(null, false);
            });
            function ClearBindModularToRole(roleid)
            {
                $.ajax({
                        type: "POST",
                        url: '@Url.Action("ClearBindMenuToRole")',
                        data: { roleid: roleid },
                        dataType: "json",
                        async: false,
                        success: function (data) {
                            if (data) {
                                rowcount = rowcount + ((data == -1) ? 0 : data);
                            }
                        },
                        error: function () { }
                });   // end of ajax
            }
        });    
    </script>
}

<div style="width:100%; text-align:center;">
    <div style="width:100%; height:400px; overflow-y: scroll;">
    <table cellpadding="0" border="0" style="width:100%;">
        <tr>
            <td colspan="2" style="background-color:#dcdcdc; 
                                   font-family:Arial 宋体; font-size:15px; font-weight:bold;">模块列表</td>
        </tr>
                @{
                    List<ModularInfo> listmodu = ViewData["allmodular"] as List<ModularInfo>;
                    for (int i = 0; i < listmodu.Count; i++)
                    {
                       <tr style="border-right:1px solid #dcdcd;">
                        <td style="text-align: right; width:20%;">
                            @listmodu[i].ModularName
                        </td>
                        <td style="text-align: left; width:78%;">
                         @{ 
                             List<MenuInfo> menulist = ViewData["allmenu"] as List<MenuInfo>;
                             var queryLowNums = from num in menulist
                                                where num.ModularId == listmodu[i].ModularId
                                                select num;
                             List<MenuInfo> menulistforid = queryLowNums.ToList();
                             for (int ii = 0; ii < menulistforid.Count; ii++)
                             {
                                 if ((ViewData["selmenu"] as List<MenuInfo>).Exists(mm => mm.NodeId == menulistforid[ii].NodeId))
                                 {                                     
                                    @Html.CheckBox(menulistforid[ii].NodeId.ToString(), true)<span>@menulistforid[ii].Title</span> 
                                 }
                                 else
                                 {
                                    @Html.CheckBox(menulistforid[ii].NodeId.ToString(), false)<span>@menulistforid[ii].Title</span>                                 
                                 }
                                 if (ii % 2 == 1)
                                 {
                                    <br />
                                 }
                             }
                          }
                        </td>
                      </tr>
                    }
                }
    </table>
    </div>
    <div style="width:550px; margin:auto; text-align:center; height:20px; border-top:1px solid #dcdcdc;">
        <a id="btnSave" href="javascript:void(0);" class="button"><span>保存</span></a> 
        <a id="btnCancel" href="javascript:void(0);" class="button"><span>关闭</span></a>          
    </div>
</div>


